/**
 * 我的资料
 * author:zhangjlt
 * date:2016年3月10日10:30:42
 */
;
define(function(require, exports, module) {
	var wechat = require("wechat");
	var template = require("template");
	var myOrder = {
		data: {},
		type: "1",
		currentPage: 1, //本月当前第几页
		pageSize: 20,
		loadedAllFlag: false,
		beginTime: "",
		endTime: "",
		/**
		 * bindEvent 页面事件
		 */
		bindEvent: function() {
			$(".orderType").unbind().on("click", function() {
				var type = $(this).attr("orderType");
				$(".orderType").removeClass("order_active")
				$(this).addClass("order_active");
				myOrder.type = type;
				myOrder.getDate(type);
				$("#main_myOrder").html("");
				myOrder.currentPage = 1;
				if($("#main_myOrder" + type).children().length == 0) {
					myOrder.loadData(type, myOrder.currentPage);
				}

			});
			//返回
			$("#return_myOrder").unbind().on("click", function() {
				window.location.href = "#/index/my";
			});
			$(document).off("click", '.orderItem').on("click", '.orderItem', function() {
				var orderId = $(this).attr("orderId");
				window.localStorage.setItem("orderId", orderId);
				window.location.href = "#/index/order_detail";
			});
		},
		getDate: function(type) {
			var myDate = new Date();
			var Month = myOrder.zeroAdd(myDate.getMonth() + 1);
			var nowData = myDate.getFullYear() + '-' + Month + '-' + myOrder.zeroAdd(myDate.getDate());
			var oldData = nowData;
			switch(type) {
				case 'month':
					oldData = myOrder.addDate(myDate, -30);
					break;
				case 'threeMonth':
					oldData = myOrder.addDate(myDate, -90);
					break;
				case 'all':
					oldData = nowData;
					break;
			}
			myOrder.beginTime = oldData + " 00:00:00";
			myOrder.endTime = nowData + " 23:59:59";
		},
		/**
		 * 进行页面路由监听配置
		 */
		bindRout: function() {
			//公共页面监听路由
			Path.map("#/index/:page_name").to(function() {
				console.log(this.params["page_name"]);
				var page_name = this.params["page_name"];
				//来自状态
				var from = "right";
				if(page_name == "my"){
					from = "left";
				}
				//记录状态
				common.loadPage({
					pageUrl: "html/" + page_name + ".html",
					reload: true,
					from: from
				});
			});
			
			Path.map("#home").to(function() {
				//返回首页
				common.loadPage({
					pageUrl: "html/index.html",
					reload: false,
					from: "left"
				});
			});
			//Path.root("#home");
			Path.listen();
		},
		/**
		 * 页面数据加载
		 */
		loadData: function(loadType, currentPage) {
			//当前页
			myOrder.currentPage = currentPage;
			//计算日期
			myOrder.getDate(loadType);
			var param = {};
			if(loadType == "all") {
				param = {
					"openId": window.localStorage.getItem("openId"),
					"appId": window.localStorage.getItem("appId"),
					"currentPage": myOrder.currentPage,
					"pageSize": myOrder.pageSize
				};
			} else {
				param = {
					"openId": window.localStorage.getItem("openId"),
					"appId": window.localStorage.getItem("appId"),
					"currentPage": myOrder.currentPage,
					"pageSize": myOrder.pageSize,
					"startTime": myOrder.beginTime,
					"endTime": myOrder.endTime
				};
			}
			common.loadData({
				url: "/wxmember/service/getOrderList",
				param: param,
				successCallBack: myOrder.getOrderListCallBack, //回调函数
				dataSource: "getOrderList",
				loadAll: myOrder.loadedAllFlag,
				cache: false 
			});

		},
		//callback function
		getOrderListCallBack: function(json) {
			console.log("调用后台ajax返回值为：" + JSON.stringify(json));
			if(json.status == "0") {
				if(json.data && json.data.list && json.data.list.length > 0) {
					var html = template('myOrderTemplate', json);
					$("#main_myOrder").append(html);
					
					//iscroll滚动加入
					var iscroll = require("iscroll");
					common.initIscroll({
						container: "iscrollContent_myOrder",
						showScrollBar: true,
						pullUpActionCallBack: myOrder.pullUpActionCallBack,
						pullDownActionCallBack:myOrder.pullDownActionCallBack,
						loadAll:myOrder.loadedAllFlag
					});
				}else{
					if(myOrder.currentPage == 1) {
						$("#main_myOrder").height(document.documentElement.clientHeight-$("#nav_myOrder").height());
						common.show_nodata("当前没有消费订单信息", "main_myOrder");
					}else{
						//两个部分共同控制加载全部
						common.loadedAll();
						myOrder.loadedAllFlag = true;
					}
				}

			} else {
				if(myOrder.currentPage == 1) {
					$("#main_myOrder").height(document.documentElement.clientHeight-$("#nav_myOrder").height());
					common.show_nodata("当前没有消费订单信息", "main_myOrder");
				}
				common.showTip({
					content: json.message
				});
			}
		},
		pullUpActionCallBack: function() {
			myOrder.currentPage += 1;
			myOrder.loadData(myOrder.currentPage);
		},
		pullDownActionCallBack:function(){
			myOrder.currentPage = 1;
			$("#main_myOrder").html("");
			myOrder.loadedAllFlag=false;
			myOrder.loadData();
		},
		addDate: function(date, days) {
			var d = new Date(date);
			d.setDate(d.getDate() + days);
			var m = d.getMonth() + 1;
			return d.getFullYear() + '-' + this.zeroAdd(m) + '-' + this.zeroAdd(d.getDate());
		},
		zeroAdd: function(s) {
			return s < 10 ? '0' + s : s;
		},
		/**
		 * each page init mothed
		 */
		initialize: function() {
			
			this.currentPage = 1;
			this.loadedAllFlag = false;
            if(common.isWeiXin()){
            	$("#nav_myOrder").css("top","0rem");
            	$("#iscrollContent_myOrder").css("top","1.9rem");
            }
			//动态加载数据
			myOrder.loadData("month", 1);
			//事件绑定
			myOrder.bindEvent();
			//路由绑定
			myOrder.bindRout();
		}
	};

	module.exports = myOrder;
});